/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javanetworking;

import GUI.Form;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.Writer;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class Listen implements Runnable, Serializable {

    DatagramSocket socket;
    private List<String> lstChunk;
    public Listen(DatagramSocket csocket) {
        this.socket = csocket;
        this.lstChunk= null;
    }

    @Override
    public void run() {
        try {
            
            while (true) {
                byte[] rcvbuf = new byte[512];
                String stRcv;
                DatagramPacket rcvpkt = new DatagramPacket(rcvbuf, rcvbuf.length);
                socket.receive(rcvpkt);
                ByteArrayInputStream bis = new ByteArrayInputStream(rcvpkt.getData());
                ObjectInputStream ois = new ObjectInputStream(bis);
                Squery q = (Squery) ois.readObject();
                //System.out.print(q.getFilename());

                if (q.getCode() == 2) {
                    File fi=new File(q.getFilename()+".chunk");
                    BufferedWriter bw=new BufferedWriter(new FileWriter(fi));
                    q.setCode(1);
                    FILES firev = new FILES(q.getFilename());
                    firev.setTenchnk(q.getListChunk());
                    String tmp=rcvpkt.getAddress().getHostAddress();
                    System.out.println("==========="+tmp);
                    for (int i = 0; i < q.getListChunk().size(); i++) {
                        //System.out.println("\nCP: "+firev.getTenchnk().get(i));
                        bw.write(rcvpkt.getAddress().getHostAddress()+":"+rcvpkt.getPort()+":"+firev.getTenchnk().get(i));
                        bw.write("\n");
                    }
                    bw.close();
                    

//                        new Thread(new javanetworking.Send(socket, rcvpkt.getAddress().getHostAddress(), rcvpkt.getPort(), q.toByteArray())).start();
                } else if (q.getCode() == 0) {
                    q.setCode(2);
                    FILES ff = new FILES(q.getFilename());
                    ff.loadchunk();
                    q.setListChunk(ff.getTenchnk());
                    try {
                        new Thread(new javanetworking.Send(socket, rcvpkt.getAddress().getHostAddress(), rcvpkt.getPort(), q.toByteArray())).start();
                    } catch (Exception ex) {
                        Logger.getLogger(Listen.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            }
        } catch (SocketException ex) {
            Logger.getLogger(Listen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Listen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Listen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
    public  List<String> getLstChunk()
    {
        return this.lstChunk;
    }
}
